const IndexModel = require('../models/IndexModel')

class Index {
    /**
     * 更新导航条数据
     * @param ctx title            文章标题
     * @param ctx introduction     文章简介
     * @param ctx categoryId       文章分类ID
     * @param ctx tag              文章标签
     * @param ctx cover            文章封面
     * @param ctx content          文章内容
     *
     * @returns 更新成功则返回更新后的文章数据，失败返回更新失败的原因
     */
    static async update(ctx) {
        let {id} = ctx.params;

        // 检测是否传入ID
        if (!id) {
            ctx.response.status = 412;
            ctx.body = {
                code: 412,
                message: `ID不能为空`
            }

            return false;
        }

        if (isNaN(id)) {
            ctx.response.status = 412;
            ctx.body = {
                code: 412,
                message: `请传入正确的ID`
            }

            return false;
        }

        // 接收参数
        let {school_content, school_content_img, notice_article, banner1, notice_category,consultation, consultation_b,banner, service_items,service_items_b, end_date, img_model1, img_model2, xtp_model1,xtp_model2, schools, educations,logo,cr_education} = ctx.request.body;

        let params = {
            school_content,
            school_content_img,
            notice_article,
            notice_category,
            banner,
            service_items,
            service_items_b,
            end_date,
            img_model1,
            img_model2,
            schools,
            educations,
            xtp_model1,
            xtp_model2,
            consultation,
            consultation_b,
            banner1,
            cr_education,
            logo
        }

        try {
            await IndexModel.update(id, params);


            ctx.response.status = 200;
            ctx.body = {
                code: 500,
                message: `更新成功`
            }

        } catch (err) {
            ctx.response.status = 500;
            ctx.body = {
                code: 500,
                message: `更新失败`,
                data: err
            }
        }
    }

    static async detail(ctx) {
        // 文章ID
        let {id} = ctx.params;

        // 检测是否传入ID
        if (!id) {
            ctx.response.status = 412;
            ctx.body = {
                code: 412,
                message: `ID不能为空`
            }

            return false;
        }

        if (isNaN(id)) {
            ctx.response.status = 412;
            ctx.body = {
                code: 412,
                message: `请传入正确的ID`
            }

            return false;
        }

        try {

            let data = await IndexModel.detail(id);

            ctx.response.status = 200;
            ctx.body = {
                code: 200,
                message: `查询成功`,
                data
            }

        } catch (err) {
            ctx.response.status = 500;
            ctx.body = {
                code: 500,
                message: `查询失败`,
                data: err
            }
        }

    }
}

module.exports = Index
